package com.xnfh.planting.ui.fragment

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.FieldListBean
import com.xnfh.common.bean.Record
import com.xnfh.common.constant.ArouterPath
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.planting.R
import com.xnfh.planting.databinding.FragmentPlantingLayoutBinding
import com.xnfh.planting.ui.activity.PlantFieldDetailActivity
import com.xnfh.planting.ui.adapter.PlantFieldListAdapter
import com.xnfh.planting.viewmodel.PlantingViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/8 14:39
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/8 14:39
 * @UpdateRemark: 更新说明
 */
@Route(path = ArouterPath.PLANTING.PLANTING_FRAGMENT)
class PlantingFragment : BaseFragment<FragmentPlantingLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.fragment_planting_layout

    private lateinit var viewModel: PlantingViewModel
    private var page = 1
    private var adapter: PlantFieldListAdapter? = null

    override fun initView(view: View, savedInstanceState: Bundle?) {
        mBind.plantingTitle.backIv.visibility = View.GONE
        mBind.plantingTitle.headerTitleTxt.text = getString(R.string.planting_field)
        mBind.plantingRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = PlantFieldListAdapter()
        mBind.plantingRecycle.adapter = adapter
        mBind.plantingRefresh.autoRefresh()
        mBind.plantingRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.requestFieldList(page, mBind.plantingFieldSearch.text.toString())
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.requestFieldList(page, mBind.plantingFieldSearch.text.toString())
            }
        })

        mBind.plantingFieldSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mBind.plantingRefresh.autoRefresh()
            }
            val imm = requireActivity().getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
            return@setOnEditorActionListener false
        }
        mBind.plantingFieldSearch.setOnDeleteClickListener {
            mBind.plantingRefresh.autoRefresh()
        }
        adapter?.setOnItemClickListener { adapter, view, position ->
            val record = adapter.getItem(position) as Record
            val bundle = Bundle()
            bundle.putSerializable("data",record)
            PlantFieldDetailActivity.start(requireContext(),bundle)
        }
    }

    override fun observe() {
        viewModel = PlantingViewModel()
        viewModel.fieldListData.observe(this, object : BaseStateObserver<FieldListBean>(null) {
            override fun getRespDataSuccess(it: FieldListBean) {
                super.getRespDataSuccess(it)
                if (it.total <= 0) {
                    adapter?.setNewInstance(null)
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                } else {
                    mBind.plantingRefresh.setNoMoreData(it.records.isNotEmpty())
                    if (it.pages.toInt() == 1) {
                        adapter?.setNewInstance(it.records.toMutableList())
                    } else {
                        adapter?.addData(it.records)
                    }
                    page = it.pages.toInt()
                }
                //关闭刷新状态
                mBind.plantingRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                //关闭刷新状态
                mBind.plantingRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
    }
}